﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StoreWeb.ajax
{
    public partial class dictionary : System.Web.UI.Page
    {
        Maticsoft222.BLL.sqlexec sql = new Maticsoft222.BLL.sqlexec();
        Maticsoft.BLL.Dictionary bll = new Maticsoft.BLL.Dictionary();
        Maticsoft.BLL.Inventory blli = new Maticsoft.BLL.Inventory();
        Maticsoft.BLL.DictionaryrReference dbll = new Maticsoft.BLL.DictionaryrReference();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int row = 0;

                if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "getparent")
                {
                    Response.Write(JsonConvert.SerializeObject(sql.exsql("select parentcode,parentname from dbo.Dictionary where parentcode!='' and parentname!='' group by parentcode,parentname").Select(c => new { parentcode = c.Code, parentname = c.Name }).ToList()));
                }
                else
                {
                    if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "delete")
                    {
                        Maticsoft.Model.Dictionary md = bll.GetModel(Convert.ToInt32(Request["id"]));
                        //验证面料信息中是否被引用
                        if (blli.GetModelList("dictionarycode='" + md.code + "'").Count > 0)
                            Response.Write("3");
                        else
                            Response.Write((bll.Delete(Convert.ToInt32(Request["id"])) ? "1" : "0"));
                    }
                    else
                    {
                        Maticsoft.Model.Dictionary md = new Maticsoft.Model.Dictionary();
                        if (!string.IsNullOrEmpty(Request["id"]))
                        {
                            md = bll.GetModel(Convert.ToInt32(Request["id"]));
                            md.parentcode = Request["parentcode"];
                            md.parentname = Request["parentname"];
                            md.code = Request["code"];
                            md.name = Request["name"];
                            md.note = Request["note"];
                            if (bll.GetModelList("parentcode='" + Request["parentcode"] + "' and code='" + Request["code"] + "'").Count > 0 && Request["code"] != md.code)
                                row = 3;
                            else
                            {
                                md.code = Request["code"];
                                row = bll.Update(md) ? 2 : 0;
                            }
                        }
                        else
                        {
                            md.createtime = DateTime.Now;
                            md.note = Request["note"];
                            md.parentcode = Request["parentcode"];
                            md.parentname = Request["parentname"];
                            md.code = Request["code"];
                            md.name = Request["name"];
                            md.isdel = 0;
                            if (bll.GetModelList("parentcode='" + Request["parentcode"] + "' and code='" + Request["code"] + "'").Count > 0)
                                row = 3;
                            else
                                row = bll.Add(md) > 0 ? 1 : 0;
                        }
                        if (row == 1)
                        {
                            Response.Write("1");
                        }
                        else if (row == 2)
                        {
                            Response.Write("2");
                        }
                        else if (row == 3)
                        {
                            Response.Write("3");
                        }
                        else
                        {
                            Response.Write("0");
                        }
                    }
                }
            }
        }
    }
}